package cn.senjoeson.healthsport.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.adapter.MyPagerAdapter;
import cn.senjoeson.healthsport.fragment.CheckOutFragment;
import cn.senjoeson.healthsport.fragment.MeFragment;
import cn.senjoeson.healthsport.fragment.SportDataFragment;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.tablayout)
    TabLayout mTablayout;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    String[] titles = new String[]{"运动数据", "签到", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        myPagerAdapter.setFragments(initFragments());
        myPagerAdapter.setTabTitles(titles);
        mViewpager.setAdapter(myPagerAdapter);
        mTablayout.setupWithViewPager(mViewpager, false);
        mTablayout.setTabTextColors(R.color.hanks_HitColor,R.color.color_one);
        mViewpager.setOffscreenPageLimit(3);


        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mToolbar.setTitle(titles[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private List<Fragment> initFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new SportDataFragment());
        fragments.add(new CheckOutFragment());
        fragments.add(new MeFragment());
        return fragments;
    }


}
